﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZG_member.BLL;
using System.Configuration;

namespace ZG_member
{
    public partial class BuyGoods : Form
    {
        public BuyGoods()
        {
            InitializeComponent();
            skinEngine1.SkinFile = Application.StartupPath + ConfigurationManager.AppSettings["Skin"];
        }
        ProduceForm Form;
        public BuyGoods(ProduceForm Form1)
        {
            InitializeComponent();
            Form = Form1;
            height = this.Height;
            width = this.Width;
            this.Opacity = 0;
            skinEngine1.SkinFile = Application.StartupPath + ConfigurationManager.AppSettings["Skin"];
        }
        int height;
        int width;

        public string goodsTypeId;
        public string lowestOff;
        public string unit;
        private void BuyGoods_Load(object sender, EventArgs e)
        {

            
            this.timer1.Start();
            txtamount.Text = "0";
            if (Form.down == true)
            {
                this.Text = "商品退货";
                label6.Text = "退货数量:";
            }
            else
            {
                label6.Text = "进货数量:";
            }
           
        }
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (txtamount.Text == "")
            {
                txtamount.Text = "0";
            }
            goods good = new goods();
            Model.goods goodModel = new Model.goods();
            goodModel = good.GetModelList("goodsId='" + labgoodsId.Text + "'")[0];
            if (Form.down == true)
            {
                goodModel.amount = int.Parse(labamount.Text) - int.Parse(txtamount.Text);
                if (goodModel.amount < 0)
                {
                    MessageBox.Show("库存量不足！"); return;
                }

            }
            else
            {
                goodModel.amount = int.Parse(labamount.Text) + int.Parse(txtamount.Text);
            }
            if (good.Update(goodModel))
            {
                //商品流水信息
                    goodsflowing goods = new goodsflowing();
                    Model.goodsflowing goodsModel = new Model.goodsflowing();
                   
                    goodsModel.username = MainForm.Operator;//用户名
                    goodsModel.Time = DateTime.Now;

                if (Form.down == true)//退货
                {
                    labamount.Text = (int.Parse(labamount.Text) - int.Parse(txtamount.Text)).ToString();
                   
                    goodsModel.operate = "商品退货";
                    
                }
                else//进货
                {
                    labamount.Text = (int.Parse(labamount.Text) + int.Parse(txtamount.Text)).ToString();
                   
                    goodsModel.operate = "商品进货";
                }
               //商品流水信息
                goodsModel.goodsId = labgoodsId.Text;//商品编号
                goodsModel.goodsName = labgoodsName.Text; //商品名称
                goodsModel.unit = unit;////
                goodsModel.price = decimal.Parse(labprice.Text);//预售单价
                goodsModel.finalCost = Convert.ToDouble(labfinalCost.Text);//成本
                goodsModel.amount = int.Parse(txtamount.Text);//数量
                goodsModel.Promotionprice = "100";//折扣
                //goodsModel.lowestOff = lowestOff;////
                goodsModel.goodsTypeId = goodsTypeId;////

                int row = goods.Add(goodsModel);
                if (row <= 0)
                {
                    MessageBox.Show("系统出现故障！请检查."); return;
                }








                goods goodd = new goods();
                DataTable dtt = goodd.GetList("").Tables[0];
                Form.dgvgoods.DataSource = dtt;
                Form.dgvgoods.AllowUserToAddRows = false;
                foreach (DataGridViewRow dgv in Form.dgvgoods.Rows)
                {
                    goodsType goodtype = new goodsType();
                    Model.goodsType goodtypeModel = new Model.goodsType();
                    goodtypeModel = goodtype.GetModelList("id='" + dgv.Cells["goodsTypeId"].Value + "'")[0];
                    dgv.Cells["typeName1"].Value = goodtypeModel.typeName;
                    //if (dgv.Cells["GlobalDiscount"].Value.ToString() == "False")
                    //{
                    //    dgv.Cells["GlobalDiscount2"].Value = "否";
                    //}
                    //else
                    //{
                    //    dgv.Cells["GlobalDiscount2"].Value = "是";
                    //}
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            timer2.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Left += 2;
            this.Opacity += 0.1;
            if (Opacity == 1)
            {
                this.timer1.Stop();

            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            this.Top = this.Top + 10;
            this.Opacity = this.Opacity - 0.06;
            if (Opacity <= 0)
            {
                this.timer2.Stop();
                this.Close();
            }
        }
        int flag = 0;
        private void BuyGoods_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (flag == 0)
            {
                e.Cancel = true;
                flag = 1;
                this.timer2.Start();
            }
        }

        private void txtamount_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(((e.KeyChar >= '0') && (e.KeyChar <= '9')) || e.KeyChar <= 31))
            {
                //if (e.KeyChar == '.')
                //{
                //    if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                //        e.Handled = true;
                //}
                //else
                e.Handled = true;
            }
            else
            {
                if (e.KeyChar <= 31)
                {
                    e.Handled = false;
                }
                else if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                {
                    if (((TextBox)sender).Text.Trim().Substring(((TextBox)sender).Text.Trim().IndexOf('.') + 1).Length >= 4)
                        e.Handled = true;
                }
            }
        }

    }
}